import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    // user: {
    //   u_avatar_url: window.localStorage.getItem('user') === null ? '' : window.localStorage.getItem('user').u_avatar_url,
    //   u_id: window.localStorage.getItem('user') === null ? '' : window.localStorage.getItem('user').u_id,
    //   u_nickname: window.localStorage.getItem('user') === null ? '' : window.localStorage.getItem('user').u_nickname,
    //   u_password: window.localStorage.getItem('user') === null ? '' : window.localStorage.getItem('user').u_password,
    //   u_role_id: window.localStorage.getItem('user') === null ? '' : window.localStorage.getItem('user').u_role_id,
    //   u_true_name: window.localStorage.getItem('user') === null ? '' : window.localStorage.getItem('user').u_true_name
    // },
    user: window.localStorage.getItem('user') === null ? {} : JSON.parse(window.localStorage.getItem('user')),
    token: window.localStorage.getItem('token') !== null
  },
  mutations: {
    login (state, user) {
      // alert('往store存入用户登录信息' + JSON.stringify(user))
      state.user = user
      window.localStorage.setItem('user', JSON.stringify(user))
      window.localStorage.setItem('token', true)
      this.state.token = true
    },
    getUser () {
      return this.state.user.person_name
    },
    getUserID () {
      return this.state.user.u_id
    },
    logout () {
      this.state.user.u_avatar_url = ''
      this.state.user.u_id = ''
      this.state.user.u_nickname = ''
      this.state.user.u_password = ''
      this.state.user.u_role_id = ''
      this.state.user.u_true_name = ''
      this.state.token = false
      localStorage.removeItem('user')
      window.localStorage.setItem('token', false)
      console.log('logout: ' + JSON.stringify(this.state.user) + ' and state.token = ' + this.state.token)
    },
    isLogin () {
      return this.state.token === true
    }
  }
})
